package com.yingyi.basic.controller;


import com.yingyi.basic.exception.BusinessException;
import com.yingyi.basic.service.IVerifyCodeService;
import com.yingyi.basic.util.JsonResult;
import com.yingyi.user.dto.BinderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.Map;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 获取图形验证码 Base64位编码的字符串形式
     *
     * @return
     */
    @GetMapping("/image/{key}")
    public JsonResult getVerifyCode(@PathVariable(value = "key") String key){
        try {
            String base64Str = verifyCodeService.getVerifyCode(key);
            return JsonResult.me().setResultObj(base64Str);

        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMessage(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("获取图形验证码失败，请稍后重试");
        }
    }


    /**
     * 获取短信验证码
     *
     * 参数接收：
     *  1 定义一个dto类来接收
     *  2 使用map接收参数
     * @return
     */
    @PostMapping("/smsCode")
    public JsonResult sendSmsCode(@RequestBody Map<String, String> map){
        try {
            verifyCodeService.sendSmsCode(map);
            return JsonResult.me();
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("系统繁忙，请稍后重试");
        }

    }

    //binderSmsCode
    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody BinderDto dto) {
        try {
            verifyCodeService.binderSmsCode(dto);
            return JsonResult.me();
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("系统繁忙，请稍后重试");
        }

    }

}
